package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.nls.service.dao.SDicTreeDao;
import com.fdb.efp.nls.service.domain.BizParamValue;
import com.fdb.efp.nls.service.domain.SDicTree;
import com.fdb.efp.nls.service.facade.SDicTreeService;
import com.fdb.efp.nls.service.vo.BizParamValueVO;
import com.fdb.efp.nls.service.vo.SDicTreeVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * ServiceImpl: 系统字典树表
 * <p>
 * 
 * @author fuzm
 * @date 2018-05-15
 */
@Service("sDicTreeService")
public class SDicTreeServiceImpl implements SDicTreeService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(SDicTreeServiceImpl.class);

	@Autowired
	private SDicTreeDao sDicTreeDao;

	/**
	 * 新增操作
	 */
	public int insertSDicTree(SDicTreeVO inSDicTreeVo) {
		logger.debug("当前新增数据为:" + inSDicTreeVo.toString());
		int num = 0;
		try {
			SDicTree sDicTree = new SDicTree();
			beanCopy(inSDicTreeVo, sDicTree);
			num = sDicTreeDao.insertSDicTree(sDicTree);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	
	//批量新增禁入行业
	public int insertMoreBizParamValue(List<BizParamValueVO> inBizParamValueVO) {
		logger.debug("当前新增数据为:" + inBizParamValueVO.toString());
		int num = 0;
		try {
			List<BizParamValue> bizParamValue = new ArrayList<BizParamValue>();
			beanCopy(inBizParamValueVO, bizParamValue);
			num = sDicTreeDao.insertMoreBizParamValue(bizParamValue);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(SDicTreeVO inSDicTreeVo) {
		logger.debug("当前删除数据条件为:" + inSDicTreeVo);
		int num = 0;
		try {
			SDicTree sDicTree = new SDicTree();
			beanCopy(inSDicTreeVo, sDicTree);
			num = sDicTreeDao.deleteByPk(sDicTree);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inSDicTreeVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 禁入行业全表删除
	 */
	public int deleteAllBizParamValue(BizParamValueVO inBizParamValueVO) {
		logger.debug("当前删除数据条件为:" + inBizParamValueVO);
		int num = 0;
		try {
			BizParamValue bizParamValue = new BizParamValue();
			beanCopy(inBizParamValueVO, bizParamValue);
			num = sDicTreeDao.deleteAllBizParamValue(bizParamValue);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inBizParamValueVO + "删除的数据条数为" + num);
		return num;
	}
	/**
	 * 批量删除禁入行业
	 */
	public int deleteMoreBizParamValue(List<BizParamValueVO> inBizParamValueVO) {
		logger.debug("当前删除数据条件为:" + inBizParamValueVO);
		int num = 0;
		try {
			BizParamValue bizParamValue = new BizParamValue();
			beanCopy(inBizParamValueVO, bizParamValue);
			StringBuffer sb = new StringBuffer();
			if(inBizParamValueVO != null && inBizParamValueVO.size()>0){
				for (BizParamValueVO bizParamValueVO : inBizParamValueVO) {
					sb.append("'"+bizParamValueVO.getEnname()+"'"+",") ;
				}
				String str = sb.substring(0, sb.length()-1);
				num = sDicTreeDao.deleteMoreBizParamValue(str);
			}
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inBizParamValueVO + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 根据双主键删除禁入行业配置
	 */
	public int deleteByValueAndEnname(BizParamValueVO inBizParamValueVO) {
		logger.debug("当前删除数据条件为:" + inBizParamValueVO);
		int num = 0;
		try {
			BizParamValue bizParamValue = new BizParamValue();
			beanCopy(inBizParamValueVO, bizParamValue);
			num = sDicTreeDao.deleteByValueAndEnname(bizParamValue);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inBizParamValueVO + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	public int updateByPk(SDicTreeVO inSDicTreeVo) {
		logger.debug("当前修改数据为:" + inSDicTreeVo.toString());
		int num = 0;
		try {
			SDicTree sDicTree = new SDicTree();
			beanCopy(inSDicTreeVo, sDicTree);
			num = sDicTreeDao.updateByPk(sDicTree);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inSDicTreeVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	public SDicTreeVO queryByPk(SDicTreeVO inSDicTreeVo) {

		logger.debug("当前查询参数信息为:" + inSDicTreeVo);
		try {
			SDicTree querySDicTree = new SDicTree();
			beanCopy(inSDicTreeVo, querySDicTree);
			SDicTree queryRslSDicTree = sDicTreeDao.queryByPk(querySDicTree);
			if (Objects.nonNull(queryRslSDicTree)) {
				SDicTreeVO outSDicTreeVo = beanCopy(queryRslSDicTree, new SDicTreeVO());
				logger.debug("当前查询结果为:" + outSDicTreeVo.toString());
				return outSDicTreeVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SDicTreeVO> queryAllOwner(SDicTreeVO sDicTreeVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<SDicTreeVO> list = null;
		try {
			List<SDicTree> sDicTrees = sDicTreeDao.queryAllOwnerByPage(sDicTreeVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + sDicTrees.size());
			pageSet(sDicTrees, sDicTreeVo);
			list = (List<SDicTreeVO>) beansCopy(sDicTrees, SDicTreeVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SDicTreeVO> queryAllCurrOrg(SDicTreeVO sDicTreeVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<SDicTree> sDicTrees = sDicTreeDao.queryAllCurrOrgByPage(sDicTreeVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + sDicTrees.size());
		List<SDicTreeVO> list = null;
		try {
			pageSet(sDicTrees, sDicTreeVo);
			list = (List<SDicTreeVO>) beansCopy(sDicTrees, SDicTreeVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SDicTreeVO> queryAllCurrDownOrg(SDicTreeVO sDicTreeVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<SDicTree> sDicTrees = sDicTreeDao.queryAllCurrDownOrgByPage(sDicTreeVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + sDicTrees.size());
		List<SDicTreeVO> list = null;
		try {
			pageSet(sDicTrees, sDicTreeVo);
			list = (List<SDicTreeVO>) beansCopy(sDicTrees, SDicTreeVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SDicTreeVO> queryAll() {
		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		SDicTreeVO sDicTreeVO = new SDicTreeVO();
		sDicTreeVO.setOpttype("STD_GB_4754-2002");
		List<SDicTree> sDicTrees = sDicTreeDao.querySDicTreeByOpttype(sDicTreeVO);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + sDicTrees.size());
		List<SDicTreeVO> list = null;
		try {
			list = (List<SDicTreeVO>) beansCopy(sDicTrees, SDicTreeVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	/**
	 * 查询禁入行业操作
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<BizParamValueVO> queryAllBizValueByPage(BizParamValueVO inBizParamValueVO) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<BizParamValue> bizParamValues = sDicTreeDao.queryAllBizValueByPage(inBizParamValueVO);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + bizParamValues.size());
		List<BizParamValueVO> list = null;
		try {
			pageSet(bizParamValues, inBizParamValueVO);
			list = (List<BizParamValueVO>) beansCopy(bizParamValues, BizParamValueVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BizParamValueVO> queryAllBizValue(BizParamValueVO inBizParamValueVO) {
		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		 List<BizParamValue> bizParamValues	= sDicTreeDao.queryAllBizValue(inBizParamValueVO);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + bizParamValues.size());
		List<BizParamValueVO> list = null;
		try {
			list = (List<BizParamValueVO>) beansCopy(bizParamValues, BizParamValueVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	@Override
	public int queryByValueAndEnname(BizParamValueVO inBizParamValueVO) {

		logger.debug("当前查询参数信息为:" + inBizParamValueVO);
		int num = 0;
		try {
			BizParamValue bizParamValue = new BizParamValue();
			beanCopy(inBizParamValueVO, bizParamValue);
			num = sDicTreeDao.queryByValueAndEnname(bizParamValue);

		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return num;
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<SDicTreeVO> querySDicTreeByOpttype(String opttype) {
		logger.debug("当前查询字典树的参数信息为,opttype:" + opttype);
		SDicTreeVO sDicTreeVO = new SDicTreeVO();
		sDicTreeVO.setOpttype(opttype);
		List<SDicTree> sDicTrees = sDicTreeDao.querySDicTreeByOpttype(sDicTreeVO);
		logger.debug("当前查询字典树的结果集数量为:" + sDicTrees.size());
		List<SDicTreeVO> list = null;
		try {
			list = (List<SDicTreeVO>) beansCopy(sDicTrees, SDicTreeVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}
}
